// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-10-23 14:45:50
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-11-06 14:08:08
 * @FilePath: /SGSim/DBManager/UnitTest/HDF5/Input/CoordinateServiceTest.cpp
 */

#include <memory>
#include <vector>

#include <gtest/gtest.h>



#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Coordinate/ICORD1CService.h"
#include "DBManager/IService/Input/Coordinate/ICORD1RService.h"
#include "DBManager/IService/Input/Coordinate/ICORD1SService.h"
#include "DBManager/IService/Input/Coordinate/ICORD2CService.h"
#include "DBManager/IService/Input/Coordinate/ICORD2RService.h"
#include "DBManager/IService/Input/Coordinate/ICORD2SService.h"
#include "DBManager/IService/Input/Coordinate/ICORDSummaryService.h"
#include "DBManager/UnitTest/HDF5/TestDB.h"

using namespace SG::DataStructure::FEM;

class I_CoordinateTest : public testing::Test
{};

TEST_F (I_CoordinateTest, CORD2RTest)
{
    SG::DataStructure::Common::Id const id = 1;

    // 创建对象
    SG::DataStructure::FEM::CORD2RData obj;
    obj.m_id = id;

    // 获取服务
    auto service = DB::ServiceFactory->get<SG::DBManager::ICORD2RService> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->FindById (id, obj), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (obj.m_id, id);
    ASSERT_EQ (service->GetAllId ().size (), 1);
}

TEST_F (I_CoordinateTest, CORD1RTest)
{
    SG::DataStructure::Common::Id const id = 2;

    // 创建对象
    SG::DataStructure::FEM::CORD1RData obj;
    obj.m_id = id;

    // 获取服务
    auto service = DB::ServiceFactory->get<SG::DBManager::ICORD1RService> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->FindById (id, obj), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (obj.m_id, id);
    ASSERT_EQ (service->GetAllId ().size (), 1);
}

TEST_F (I_CoordinateTest, CORD2CTest)
{
    SG::DataStructure::Common::Id const id = 4;

    // 创建对象
    SG::DataStructure::FEM::CORD2CData obj;
    obj.m_id = id;

    // 获取服务
    auto service = DB::ServiceFactory->get<SG::DBManager::ICORD2CService> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->FindById (id, obj), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (obj.m_id, id);
    ASSERT_EQ (service->GetAllId ().size (), 1);
}

TEST_F (I_CoordinateTest, CORD1CTest)
{
    SG::DataStructure::Common::Id const id = 5;

    // 创建对象
    SG::DataStructure::FEM::CORD1CData obj;
    obj.m_id = id;

    // 获取服务
    auto service = DB::ServiceFactory->get<SG::DBManager::ICORD1CService> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->FindById (id, obj), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (obj.m_id, id);
    ASSERT_EQ (service->GetAllId ().size (), 1);
}

TEST_F (I_CoordinateTest, CORD2STest)
{
    SG::DataStructure::Common::Id const id = 6;

    // 创建对象
    SG::DataStructure::FEM::CORD2SData obj;
    obj.m_id = id;

    // 获取服务
    auto service = DB::ServiceFactory->get<SG::DBManager::ICORD2SService> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->FindById (id, obj), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (obj.m_id, id);
    ASSERT_EQ (service->GetAllId ().size (), 1);
}

TEST_F (I_CoordinateTest, CORD1STest)
{
    SG::DataStructure::Common::Id const id = 7;

    // 创建对象
    SG::DataStructure::FEM::CORD1SData obj;
    obj.m_id = id;

    // 获取服务
    auto service = DB::ServiceFactory->get<SG::DBManager::ICORD1SService> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->FindById (id, obj), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (obj.m_id, id);
    ASSERT_EQ (service->GetAllId ().size (), 1);
}

TEST_F (I_CoordinateTest, SummaryTest)
{
    // 获取服务
    auto service = DB::ServiceFactory->get<SG::DBManager::ICORDSummaryService> ();

    // 添加对象
    std::vector<std::shared_ptr<SG::DataStructure::FEM::ICoordinate>> const aaa = service->GetAllCoordinate ();

    ASSERT_EQ (service->Size (), aaa.size ());
    ASSERT_EQ (6, aaa.size ());
}

#include "DBManager/IService/Input/Coordinate/ITRANSFORMATIONService.h"
TEST_F (I_CoordinateTest, TRANSFORMATIONTest)
{
    // DB::ServiceFactory->ClearDatabase ();

    auto service = DB::ServiceFactory->get<SG::DBManager::ITRANSFORMATIONService> ();

    SG::DataStructure::FEM::TRANSFORMATIONData data;
    data.m_id        = 1;
    data.m_TYPE      = SG::DataStructure::FEM::TRANSFORMATIONData::Type::Cylindrical;
    data.m_IDATAList = { { 11 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 }, { 12 } };
    data.m_RDATAList = { { 21 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 }, { 12 } };
    data.m_domainId  = 100;
    ASSERT_EQ (service->Add (data), SG::DataStructure::Common::Status::STATUS_Success);

    data.m_id        = 2;
    data.m_TYPE      = SG::DataStructure::FEM::TRANSFORMATIONData::Type::Spherical;
    data.m_IDATAList = { { 31 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 }, { 12 } };
    data.m_RDATAList = {};
    data.m_domainId  = 100;
    ASSERT_EQ (service->Add (data), SG::DataStructure::Common::Status::STATUS_Success);

    data.m_id        = 3;
    data.m_TYPE      = SG::DataStructure::FEM::TRANSFORMATIONData::Type::Spherical;
    data.m_IDATAList = {};
    data.m_RDATAList = { { 41 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 }, { 12 } };
    data.m_domainId  = 100;
    ASSERT_EQ (service->Add (data), SG::DataStructure::Common::Status::STATUS_Success);

    SG::DataStructure::FEM::TRANSFORMATIONData _OUT outData;
    ASSERT_EQ (service->FindById (1, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 1);
    ASSERT_EQ (outData.m_TYPE, SG::DataStructure::FEM::TRANSFORMATIONData::Type::Cylindrical);
    ASSERT_EQ (outData.m_IDATAList.size (), 12);
    ASSERT_EQ (outData.m_RDATAList.size (), 12);
    ASSERT_EQ (outData.m_domainId, 100);
    ASSERT_EQ (outData.m_IDATAList,
               std::vector<SG::DataStructure::FEM::TRANSFORMATIONData::IDATAData> (
                   { { 11 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 }, { 12 } }));
    ASSERT_EQ (outData.m_RDATAList,
               std::vector<SG::DataStructure::FEM::TRANSFORMATIONData::RDATAData> (
                   { { 21 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 }, { 12 } }));

    ASSERT_EQ (service->FindById (2, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 2);
    ASSERT_EQ (outData.m_TYPE, SG::DataStructure::FEM::TRANSFORMATIONData::Type::Spherical);
    ASSERT_EQ (outData.m_IDATAList.size (), 12);
    ASSERT_EQ (outData.m_RDATAList.size (), 0);
    ASSERT_EQ (outData.m_domainId, 100);
    ASSERT_EQ (outData.m_IDATAList,
               std::vector<SG::DataStructure::FEM::TRANSFORMATIONData::IDATAData> (
                   { { 31 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 }, { 12 } }));

    ASSERT_EQ (service->FindById (3, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 3);
    ASSERT_EQ (outData.m_TYPE, SG::DataStructure::FEM::TRANSFORMATIONData::Type::Spherical);
    ASSERT_EQ (outData.m_IDATAList.size (), 0);
    ASSERT_EQ (outData.m_RDATAList.size (), 12);
    ASSERT_EQ (outData.m_domainId, 100);
    ASSERT_EQ (outData.m_RDATAList,
               std::vector<SG::DataStructure::FEM::TRANSFORMATIONData::RDATAData> (
                   { { 41 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 }, { 12 } }));

    ASSERT_EQ (service->Size (), 3);
    ASSERT_EQ (service->GetAllId ().size (), 3);
}
